package co.yixiang.yshop.module.crm.service.basestorage;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.pojo.PageParam;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.util.object.BeanUtils;
import co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils;
import co.yixiang.yshop.module.crm.controller.admin.basestorage.vo.*;
import co.yixiang.yshop.module.crm.dal.dataobject.basestorage.BaseStorageDO;
import co.yixiang.yshop.module.crm.dal.mysql.basestorage.BaseStorageMapper;
import co.yixiang.yshop.module.system.dal.dataobject.user.AdminUserDO;
import co.yixiang.yshop.module.system.service.user.AdminUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.Valid;

import java.time.LocalDateTime;
import java.util.*;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.crm.enums.ErrorCodeConstants.*;

/**
 * 仓库 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class BaseStorageServiceImpl implements BaseStorageService {

    @Resource
    private BaseStorageMapper storageMapper;
    @Resource
    private AdminUserService adminUserService;

    @Override
    public Integer createStorage(@Valid BaseStorageSaveReqVO createReqVO) {
        // 插入
        BaseStorageDO storage = BeanUtils.toBean(createReqVO, BaseStorageDO.class);
        if (ObjectUtil.isNotNull(storage.getCreateTime())) {
            storage.setCreateTime(LocalDateTime.now());
        }
        storageMapper.insert(storage);
        // 返回
        return storage.getStorageId();
    }

    @Override
    public void updateStorage(@Valid BaseStorageSaveReqVO updateReqVO) {
        // 校验存在
        validateStorageExists(updateReqVO.getStorageId());
        // 更新
        BaseStorageDO updateObj = BeanUtils.toBean(updateReqVO, BaseStorageDO.class);
        storageMapper.updateById(updateObj);
    }

    @Override
    public void deleteStorage(Integer id) {
        // 校验存在
        validateStorageExists(id);
        // 删除
        storageMapper.deleteById(id);
    }

    private void validateStorageExists(Integer id) {
        if (storageMapper.selectById(id) == null) {
            throw exception(STORAGE_NOT_EXISTS);
        }
    }

    @Override
    public BaseStorageDO getStorage(Integer id) {
        return storageMapper.selectById(id);
    }

    @Override
    public PageResult<BaseStorageDO> getStoragePage(BaseStoragePageReqVO pageReqVO) {
        Long adminId = SecurityFrameworkUtils.getLoginUserId();

        // 获取当前用户信息，判断是否为管理员
        AdminUserDO currentUser = adminUserService.getUser(adminId);
        boolean isAdministrator = currentUser != null && currentUser.getIsAdministrator() != null && currentUser.getIsAdministrator() == 1L;

        return storageMapper.selectPage(pageReqVO,adminId,isAdministrator);
    }

    @Override
    public List<BaseStorageDO> getStorageList(BaseStoragePageReqVO pageReqVO) {
        return storageMapper.selectList(pageReqVO);
    }

    @Override
    public List<BaseStorageRespVO> getStorageListForExport(BaseStoragePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseStorageDO> list = storageMapper.selectList(pageReqVO);
        return BeanUtils.toBean(list, BaseStorageRespVO.class);
    }

} 